<?php
#自定义函数文件

#自定义dump
function dump($var, $label = null, $strict = true, $echo = true) {
	$label = ($label === null) ? '' : rtrim ( $label ) . ' ';
	$charset = defined ( 'CHARSET' ) ? CHARSET : 'gb2312';
	if (strtolower ( $charset ) == 'gbk')
		$charset = 'gb2312';
	if (! $strict) {
		if (ini_get ( 'html_errors' )) {
			$output = print_r ( $var, true );
			$output = "<pre>" . $label . htmlspecialchars ( $output, ENT_QUOTES, $charset ) . "</pre>";
		} else {
			$output = $label . " : " . print_r ( $var, true );
		}
	} else {
		ob_start ();
		var_dump ( $var );
		$output = ob_get_clean ();
		if (! extension_loaded ( 'xdebug' )) {
			$output = preg_replace ( "/\]\=\>\n(\s+)/m", "] => ", $output );
			$output = '<pre>' . $label . htmlspecialchars ( $output, ENT_QUOTES, $charset ) . '</pre>';
		}
	}
	if ($echo) {
		echo ($output);
		return null;
	} else {
		return $output;
	}
}


#开启错误报告
#$type: none、normal、all
function error($type = 'normal') {
	switch ($type) {
		case 'none' :
			error_reporting ( 0 );
			break;
		case 'all' :
			error_reporting ( E_ALL );
			break;
		case 'normal' :
			error_reporting ( E_ALL ^ E_NOTICE );
			break;
	}
}


#写txt文件用，生成在根目录下
#测试ajax时常用
function diy_write($text) {
	$file = WWW_ROOT . "sites/debug.html";
	$content = $text;
	if (file_exists ( $file )) {
		$content .= file_get_contents ( $file );
	}
	file_put_contents ( $file, $content );
}

  //检查该用户是否处于该用户组
  function check_user_group($userid,$groupid) {
    global $db;
   if($userid !='' && $groupid !=''){
     $sql = "SELECT * FROM ".DB_PRE."member WHERE userid = ".$userid." AND groupid = ".$groupid;
	 $result = $db->get_one($sql);
	 return $result;
   }
  }
 
  
    // 获取子栏目id集合（22,33,33,44），by catid
  /*function get_chids($catid){
  	 global $db;
     $sql = "SELECT `catid` FROM ".DB_PRE."category  WHERE `parentid` = $catid";
	 $result = $db->select($sql);

	 foreach($result as $k=>$v){
	 	$chids .= " $v[catid], ";
	 }
	 $chids .= " $catid ";
	 
	 return $chids;
  }*/
  
  // 获取子栏目id  by aming  2012.8.18
  function subcatid($catid){
  	 global $db;
     $sql = "SELECT `arrchildid` FROM ".DB_PRE."category  WHERE `catid` = $catid";
	 $result = $db->get_one($sql);
	 return $result['arrchildid'];
  }
  
  
// 删除文件 来自互联网
function delfile($dir)
{
	if (is_dir($dir))
	{
		$dh=opendir($dir);
		while (false !== ( $file = readdir ($dh)))
		{
			if($file!="." && $file!="..")
			{
				$fullpath=$dir."/".$file;
				if(!is_dir($fullpath))
				{
					unlink($fullpath);
				}
				else
				{
					delfile($fullpath);
				}
			}
		}
		closedir($dh);
	}
}

// 删除目录 来之互联网
function deldir($dir)
{
	delfile($dir);
	if (is_dir($dir))
	{
		//dir must be empty
		rmdir($dir); 
	}
	return true;
}


// 2011.07.27

/**
* 创建目录 
*/
function mkfolder($folder){
	if(!is_dir($folder)){
		mkdir($folder, 0700);
	} 
}

/**
* 创建多级目录 
* @param string mkdirs('div/css/layout')
*/
function mkdirs($dir)  
{  
	if(!is_dir($dir))  
	{  
		if(!mkdirs(dirname($dir))){  
			return false;  
		}  
		if(!mkdir($dir,0777)){   //是 0777，意味着最大可能的访问权
			return false;  
		}  
	}  
	return true;  
}  

/* 创建一个不存在的路径，不管嵌套多少个文件夹 2011.07.27 $aming*/
function mkpath($path){

	$urls = explode('/',$path);   // 分解路径
	array_splice($urls,'-1');   // 去除文件名
	
	foreach($urls as $key=>$val){      // 将去除文件名的数组从新组合为 data/news 格式目录
		if($newpath){ $newpath = $newpath.'/'; }
		$newpath .= $val;
	}
	mkdirs($newpath);  //建立目录，可建多级目录
	
	/* 创建文件 */
	if ((!file_exists($path)) || (!is_file($path))){
		//@chmod($file,0777);
		fopen($file, 'w');
	}
	if (!is_writable($file)){
		echo "文件34;.$file.&#34;不可写，请检查其属性后重试！";
	}
	else{
		return true;
	}
}

/**
* 删除多级目录 
* @param string mkdirs('div')  div/css/layout
*/
function rmdirs($dir){  
	$d = dir($dir);  
	while (false !== ($child = $d->read())){  
		if($child != '.' && $child != '..'){  
			if(is_dir($dir.'/'.$child)) {
				rmdirs($dir.'/'.$child);
			}  
			else {
				unlink($dir.'/'.$child);
			}  
		}  
	}  
	$d->close();  
	rmdir($dir); 
} 

/**
* 删除多级路径 
* @param string mkdirs('div/css/layout')  
*/
function rmpath($path){
	//rmdirs('F:/___htdocs/cms4acc/aaa');
	echo $path;exit();
}




//
// Function: 获取远程图片并把它保存到本地 (互联网)
// 确定您有把文件写入本地服务器的权限
// 变量说明:
// $url 是远程图片的完整URL地址，不能为空。
// $filename 是可选变量: 如果为空，本地文件名将基于时间和日期
// 自动生成.

function grabimage($url,$filename="") {
	
	if(!$url) return false;

	if($filename=="") {
		$ext=strrchr($url,".");
		if($ext!=".gif" && $ext!=".jpg" && $ext!=".jpeg" && $ext!=".png" && $ext!=".bmp") return false;
		$filename=time().$ext;
	}
	
	ob_start();
	readfile($url);
	$img = ob_get_contents();
	ob_end_clean();
	$size = strlen($img);
	
	// 创建目录
	$uploadfile = "sites/files/".date('Y').'/'.date('md').'/';
	mkdirs(WWW_ROOT.$uploadfile);
	
	// 创建本地图片
	$fp=@fopen(WWW_ROOT.$uploadfile.$filename, "a");
	fwrite($fp,$img);
	fclose($fp);

	return $uploadfile.$filename;
}

// $img=grabimage("http://www.comsenz.com/themes/default/images/otherlink/2009idc.jpg","");



	
	
	
	function category_sub_count($catid){
		global $db;
		$result = $db->select("select contentid from ".DB_PRE."content where catid = $catid");
		$count = count($result);
		unset($result);
		return $count;
	}

	function comment_get_counts($keyid){
		global $db;
		$result = $db->select("select commentid from ".DB_PRE."comment where keyid = '$keyid'");
		return count($result);
	}
	
	// 总文章数量
	function content_all_count(){
		global $db;
		$result = $db->select("select contentid from ".DB_PRE."content where status = 99");
		return count($result);
	}
	
	// 今日更新文章
	function content_today_count(){
		global $db;
		$t = time() - 43200;
		$result = $db->select("select contentid from ".DB_PRE."content where status = 99 && updatetime > ".$t);
		return count($result);
	}
	// 
	function sign_all_count(){
		global $db;
		$result = $db->select("select signid from ".DB_PRE."sign where status = 99");
		return count($result);
	}
	
	// 
	function sign_today_count(){
		global $db;
		$t = time() - 43200;
		$result = $db->select("select signid from ".DB_PRE."sign where status = 99 && updatetime > ".$t);
		return count($result);
	}
	
	// 生成下载链接（半成品）
	function down($url){
		$downurl = urlencode(site_auth("c=$r[contentid]&f=$url", 'ENCODE', AUTH_KEY));
		$url = "?module=upload&action=down&url=$downurl";
		return $url;
	}
	
	
	/*屏蔽手机号码中间段*/
	function mobiencode($mobile){
		return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$mobile);
	}
	//echo mobileencode('15876588581');exit();

	
	/*屏蔽电话号码中间段*/
	function telencode($telephone){
		return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$telephone);
	}
	//echo telephoneencode('010-88888888-8');exit();
	
	
	/*屏蔽IP号码中间段*/
	function ipencode($ip){
		return preg_replace("/^([0-9]{1,3}\.[0-9]{1,3})\.[0-9]{1,3}\.[0-9]{1,3}$/", "\\1.*.*", $ip);
	}
	//echo ipencode('127.0.0.1');exit();

	
?>